/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef, useState } from 'react';
import { message, Spin } from 'antd';
import type { TableListItem } from '../../index.d';
import type { ActionType } from '@ant-design/pro-table';
import { getCloudCallColumns } from '../../index.config';
import { ModalForm } from '@ant-design/pro-form';
import ProTable from '@ant-design/pro-table';
import { addWhiteList, cloudCallDetail, setCloudCallProportion } from '@/services/yz/manage';

interface SmsCloudCall {
  children: JSX.Element;
  item: TableListItem;
  callBack?: any;
}

const KbSmsCloudCall: React.FC<SmsCloudCall> = ({ item, children }) => {
  const { dak_id: cm_id, dak_name, master_phone, phone } = item;
  const actionRef = useRef<ActionType>();
  const [loading, setLoading] = useState<boolean>(false);
  const requestTable = async () => {
    const { code, data, msg } = await cloudCallDetail({ cm_id });
    if (code == 0) {
      return {
        data: [{ ...data, id: cm_id }],
      };
    } else {
      message.error(msg);
      return {};
    }
  };
  const onSave = async (params: { notice_percentage: string }) => {
    setLoading(true);
    const { code, msg } = await setCloudCallProportion({
      cm_id,
      notice_percentage: params.notice_percentage,
    });
    if (code == 0) {
      setLoading(false);
      message.success('修改成功');
      actionRef.current?.reload();
    } else {
      message.error(msg);
    }
  };
  const onSwitch = (only_sms_auth: number) => {
    setLoading(true);
    addWhiteList({ only_sms_auth, cm_id }).then((res) => {
      const { code, msg } = res;
      if (code == 0) {
        message.success(msg);
        setLoading(false);
        actionRef.current?.reload();
      } else {
        message.error(msg);
      }
    });
  };
  return (
    <ModalForm
      title='短信云呼管理'
      trigger={children}
      width={480}
      submitter={false}
      modalProps={{
        centered: true,
        bodyStyle: { padding: 0 },
      }}
    >
      <h3 style={{ padding: 20, margin: 0 }}>
        {dak_name} {master_phone ? master_phone : phone}
      </h3>
      <Spin spinning={loading}>
        <ProTable
          rowKey='id'
          actionRef={actionRef}
          loading={false}
          request={requestTable}
          columns={getCloudCallColumns({ onSave, onSwitch })}
          scroll={{
            x: '100%',
          }}
          search={false}
          toolBarRender={false}
          pagination={false}
        />
      </Spin>
    </ModalForm>
  );
};

export default KbSmsCloudCall;
